﻿Imports ClubStarterKit.Data
Imports ClubStarterKit.Web
Imports SubSonic
Imports System.Linq
Imports System.Data.Linq


Partial Class CricketStats_Default
    Inherits System.Web.UI.Page

    Dim connectionString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("ClubDBConnectionString").ConnectionString


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim SeasonID As Integer = 0
        If Request("SeasonID") Is Nothing Then
            SeasonID = Settings.CurrentSeason
        Else
            SeasonID = Integer.Parse(Request("SeasonID"))
        End If
        If IsPostBack = False Then
            'Context to cricket schema...
            Dim context As New CricketBLL.CricketStatsDataContext(connectionString)

            Dim seas = From s In context.Seasons _
                       Select s

            SeasonsDropDown.DataTextField = "SeasonName"
            SeasonsDropDown.DataValueField = "SeasonId"

            SeasonsDropDown.DataSource = seas
            SeasonsDropDown.DataBind()

            'GET TEAMS
            Dim tms = From t In context.Teams _
                      Select t
            TeamListRepeater.DataSource = tms
            TeamListRepeater.DataBind()

            BindMatches(Settings.CurrentSeason())
        End If


        If User.IsInRole("GameRecorder") Then
            IsGameRecorderOrAdmin = True
        Else
            If User.IsInRole("Administrators") Then
                IsGameRecorderOrAdmin = True
            Else
                IsGameRecorderOrAdmin = False
            End If
        End If

        If IsGameRecorderOrAdmin = True Then
            If Request("seasonid") Is Nothing Then
                AddTeam.NavigateUrl = "~/CricketStats/newteam.aspx?seasonid=" & Settings.CurrentSeason
            End If
            AddTeam.NavigateUrl = "~/CricketStats/newteam.aspx?seasonid=" & Request("seasonid")
        End If
    End Sub

    ''' <summary>
    ''' Bind teams to repeater which fire item bound event, that handler will bind each teams game
    ''' data to a gridview
    ''' </summary>
    ''' <param name="seasonId"></param>
    ''' <remarks></remarks>
    Private Sub BindMatches(ByVal seasonId As Integer)
        'Context to cricket schema...
        Using context As New CricketBLL.CricketStatsDataContext(connectionString)

            Dim teams = From t In context.Teams _
                        Select t.TeamId, t.TeamName

            repTeamGames.DataSource = teams
            repTeamGames.DataBind()

        End Using

    End Sub

    Private Sub ConfigDropDown(ByVal selseasonname As String)
        Dim col As New SeasonsCollection()
        col.LoadAndCloseReader(Seasons.FetchAll())

        Dim season As New Seasons()

        For Each season In col
            Dim li As New ListItem(season.SimpleName, season.Id)
            If season.SimpleName = selseasonname Then
                li.Selected = True
            Else
                li.Selected = False
            End If
            SeasonsDropDown.Items.Add(li)
        Next

    End Sub

    Protected Sub SeasonsDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SeasonsDropDown.SelectedIndexChanged
        Dim selvalue As Integer = Integer.Parse(CType(sender, DropDownList).SelectedValue.ToString())
        'If selvalue = Settings.CurrentSeason Then
        '    Response.Redirect("~/CricketStats/default.aspx")
        'Else
        '    Response.Redirect("~/CricketStats/default.aspx?seasonid=" & selvalue.ToString())
        'End If

        BindMatches(selvalue)

    End Sub

    Private m_isallowed As Boolean = False
    Public Property IsGameRecorderOrAdmin() As Boolean
        Get
            Return m_isallowed
        End Get
        Set(ByVal value As Boolean)
            m_isallowed = value
        End Set
    End Property

    Protected Sub MarkAsCurrent_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MarkAsCurrent.Click
        If Request("seasonid") Is Nothing = False Then
            ClubStarterKit.Web.League.MarkSeasonAsCurrent(Request("seasonid"))
        Else
            MsgBox("ERROR")
        End If
        Response.Redirect("~/CricketStats/default.aspx")
    End Sub

    Protected Sub repTeamGames_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repTeamGames.ItemDataBound
        Using context As New CricketBLL.CricketStatsDataContext(connectionString)

            Dim gv As GridView = CType(e.Item.FindControl("gvGames"), GridView)
            Dim teamId As Integer = Integer.Parse(CType(e.Item.FindControl("hidTeamId"), HiddenField).Value)

            'GET MATCHES
            'first XI
            Dim matches = From m In context.Matches _
                          Where m.TeamId = teamId AndAlso m.SeasonId = Settings.CurrentSeason() _
                          Order By m.MatchDate Ascending _
                          Select m

            gv.DataSource = matches
            gv.DataBind()

        End Using
    End Sub
End Class